#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
sys.path.insert(0, '.')

import os
import pandas as pd

face_rec_count_file = r'../DeviceFaceRec.csv'
device_file = r'../Device.csv'


def main():
    face_rec_count_cvs = pd.read_csv(face_rec_count_file, sep=',')
    face_rec_device_ids = face_rec_count_cvs['deviceid']
    face_rec_counts = face_rec_count_cvs['detectCount']
    rider_counts = face_rec_count_cvs['riderCount']
    watch_rider_counts = face_rec_count_cvs['watchRiderCount']

    device_cvs = pd.read_csv(device_file, sep=',')
    device_ids = device_cvs['设备ID']
    xiao_qus = device_cvs['所属小区']
    xiao_qu_positions = device_cvs['小区位置']
    ai_versions = device_cvs['AI包版本号']
    onlines = device_cvs['在线情况']

    device_infos = {}
    for device_id, xiao_qus, xiao_qu_positions, ai_versions, onlines in zip(device_ids, xiao_qus, xiao_qu_positions, ai_versions, onlines):
        device_infos[device_id.strip()] = [xiao_qus.strip(), xiao_qu_positions.strip(), ai_versions.strip(), onlines.strip()]

    device_id_face_rec_counts = {}
    device_id_watch_rider_counts = {}
    device_id_rider_counts = {}
    for device_id, face_rec_count, watch_rider_count, rider_count in zip(face_rec_device_ids, face_rec_counts, watch_rider_counts, rider_counts):
        device_id = device_id.strip()
        device_id_face_rec_counts[device_id] = int(face_rec_count)
        device_id_watch_rider_counts[device_id] = int(watch_rider_count)
        device_id_rider_counts[device_id] = int(rider_count)

    device_id_face_rec_infos = []
    for device_id in face_rec_device_ids:
        face_rec_count = device_id_face_rec_counts[device_id]
        watch_rider_count = device_id_watch_rider_counts[device_id]
        rider_count = device_id_rider_counts[device_id]

        if watch_rider_count == 0:
            watch_face_rec_ratio = 0
        else:
            watch_face_rec_ratio = 100 * face_rec_count / watch_rider_count

        if rider_count == 0:
            watch_ratio = 0
            face_rec_ratio = 0
        else:
            watch_ratio = 100 * watch_rider_count / rider_count
            face_rec_ratio = 100 * face_rec_count / rider_count
        device_id_face_rec_infos.append([device_id, face_rec_count, watch_rider_count, rider_count, watch_face_rec_ratio, face_rec_ratio, watch_ratio])

    print('设备ID，识别数，观看乘梯人数，乘梯人数，识别数与观看乘梯人数比率，识别数与乘梯人数比率，观看乘梯人数与乘梯人数比率，小区与安装位置')
    device_id_face_rec_infos.sort(key=lambda x: x[4], reverse=True)
    total_face_rec_count = 0
    total_watch_rider_count = 0
    total_rider_count = 0
    for device_id, face_rec_count, watch_rider_count, rider_count, watch_face_rec_ratio, face_rec_ratio, watch_ratio in device_id_face_rec_infos:
        device_info = device_infos.get(device_id, None)
        if device_info is not None:
            total_face_rec_count += face_rec_count
            total_watch_rider_count += watch_rider_count
            total_rider_count += rider_count
            out_info = '{} {:4d} {:4d} {:4d} {:.02f}% {:.02f}% {:.02f}% {} {}'.format(device_id, face_rec_count, watch_rider_count, rider_count, watch_face_rec_ratio, face_rec_ratio, watch_ratio, device_info[0], device_info[1])
            print(out_info)
        # else:
        #     print(device_id)
    print('{:4d} {:4d} {:4d} {:.02f}% {:.02f}% {:.02f}%'.format(total_face_rec_count, total_watch_rider_count, total_rider_count, 100 * total_face_rec_count / total_watch_rider_count, 100 * total_face_rec_count / total_rider_count, 100 * total_watch_rider_count / total_rider_count))


if __name__ == '__main__':
    main()
